package com.yanqu.road.server.gameplayer.module.player;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.seasontask.SeasonTaskConditionData;
import com.yanqu.road.entity.crossorewar.CrossOreWarRecord;
import com.yanqu.road.entity.crossorewar.CrossUserOrePit;
import com.yanqu.road.entity.enums.activity.eActivityOreWarType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.orewar.*;
import com.yanqu.road.entity.player.UserOreWarPatronsData;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.task.args.OreWarActivityUnionRiseRankArgs;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.logic.bussiness.player.UserOreWarBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.orewar.OreWarProto;
import com.yanqu.road.server.cmd.orewar.task.CrossOrePatronsAbilityChangeTask;
import com.yanqu.road.server.cmd.orewar.task.PatronsAbilityChangeTask;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.pb.OreWarPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class OreWarModule extends GeneralModule {

    private Map<Integer,UserOreWarHp> userOreWarHpMap = new ConcurrentHashMap<>();

    private UserOreData userOreData;

    //activityId pid
    private Map<Integer,Map<Integer, UserOreRefuge>> userOreRefugeMap = new ConcurrentHashMap<>();

    //activityId pid
    private Map<Integer,Map<Integer, UserOreWarPatronsData>> oreWarPatronsDataMap = new ConcurrentHashMap<>();

    private Map<Integer, UserPatrons> needUpdateMap = new ConcurrentHashMap<>();

    public OreWarModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean afterLoadData() {
        if(SystemOpenMgr.systemOpen(player, eSystemId.OreWar.getValue())){
            initUserOreData(player.getUserId());
            List<ActivityInfo> activityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
            if(activityInfoList.size() > 0){
                for(ActivityInfo activityInfo : activityInfoList){
                    initUserOreWarHp(activityInfo.getActivityId());
                }
            }
            checkOreRefugeInit();
        }
        return true;
    }

    @Override
    public boolean saveData() {
        if(null != userOreData) {
            if (userOreData.isInsertOption()) {
                UserOreWarBussiness.addUserOreData(userOreData);
            } else if (userOreData.isUpdateOption()) {
                UserOreWarBussiness.updateUserOreData(userOreData);
            }
        }

        if(null != userOreWarHpMap) {
            for(UserOreWarHp userOreWarHp : userOreWarHpMap.values()){
                if (userOreWarHp.isInsertOption()) {
                    UserOreWarBussiness.addUserOreWarHp(userOreWarHp);
                } else if (userOreWarHp.isUpdateOption()) {
                    UserOreWarBussiness.updateUserOreWarHp(userOreWarHp);
                }
            }
        }

        for(Map<Integer, UserOreRefuge> userRefugeMap : userOreRefugeMap.values()) {
            List<UserOreRefuge> refugeList = new ArrayList<>(userRefugeMap.values());
            for (UserOreRefuge userOreRefuge : refugeList) {
                if (userOreRefuge.isInsertOption()) {
                    UserOreWarBussiness.addUserOreRefuge(userOreRefuge);
                } else if (userOreRefuge.isUpdateOption()) {
                    UserOreWarBussiness.updateUserOreRefuge(userOreRefuge);
                }
            }
        }

        for(Map<Integer, UserOreWarPatronsData> acMap : oreWarPatronsDataMap.values()){
            List<UserOreWarPatronsData> tempList = new ArrayList<>(acMap.values());
            for(UserOreWarPatronsData userOreWarPatronsData : tempList){
                if(userOreWarPatronsData.isInsertOption()){
                    UserOreWarBussiness.addUserOreWarPatronsData(userOreWarPatronsData);
                }else if(userOreWarPatronsData.isUpdateOption()){
                    UserOreWarBussiness.updateUserOreWarPatronsData(userOreWarPatronsData);
                }
            }
        }
        return true;
    }

    @Override
    public boolean loadData() {
        userOreData = UserOreWarBussiness.getUserOreData(player.getUserId());
        OreWarMgr.setUserOreData(userOreData);
        List<ActivityInfo> activityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
        if(activityInfoList.size() > 0){
            for(ActivityInfo activityInfo : activityInfoList){
                userOreRefugeMap.put(activityInfo.getActivityId(),UserOreWarBussiness.getUserOreRefugeMap(player.getUserId(),activityInfo.getActivityId()));
                oreWarPatronsDataMap.put(activityInfo.getActivityId(),UserOreWarBussiness.getUserOreWarPatronsDataMap(player.getUserId(),activityInfo.getActivityId()));

                UserOreWarHp userOreWarHp = UserOreWarBussiness.getUserOreWarHp(activityInfo.getActivityId(),player.getUserId());
                if(userOreWarHp == null){
                    initUserOreWarHp(activityInfo.getActivityId());
                }else{
                    userOreWarHpMap.put(activityInfo.getActivityId(),userOreWarHp);
                }
                checkOreWarPatronsMap(activityInfo.getActivityId());
            }
        }

        return true;
    }

    @Override
    public void afterLogin() {
        if(SystemOpenMgr.systemOpen(player, eSystemId.OreWar.getValue())){
            syncData();
        }
    }

    public void syncData() {
        List<ActivityInfo> activityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
        checkOreRefugeInit();//同步广场
        for(ActivityInfo activityInfo : activityInfoList){
            List<UserOreRefuge> userOreRefugeList = new ArrayList<>();
            if(null != userOreRefugeMap && userOreRefugeMap.containsKey(activityInfo.getActivityId())){
                userOreRefugeList.addAll(userOreRefugeMap.get(activityInfo.getActivityId()).values());
            }
            syncOreRefuge(userOreRefugeList);
            checkOreWarPatronsMap(activityInfo.getActivityId());
            checkPatronsDispatch(activityInfo.getActivityId());
            //这里就同步了，还没等跨服回来，跨服的回来了需要再次同步一次消失的占领，质检假如不在线是通过每次登陆修复占领数据的
            syncUserOreWarPatronsData(activityInfo.getActivityId());
            if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
                List<UserOrePit> pitList = OreWarMgr.getUserOrePitList(activityInfo.getActivityId(),player.getUserId());
                syncOrePit(activityInfo.getActivityId(),pitList, false, true);
            }else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
                OreWarProto.CrossOreGetSelfOrePitReqMsg.Builder msg = OreWarProto.CrossOreGetSelfOrePitReqMsg.newBuilder();
                msg.setActivityId(activityInfo.getActivityId());
                int groupId = GameUnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(), getUserId());
                GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C_CROSS_ORE_WAR_SELF_PIT,msg,getUserId(),activityInfo.getActivityId(),groupId);
            }
            syncUserOreWarHp(activityInfo.getActivityId());
            syncOreWarRecord();
        }
    }

    @Override
    public void loginSendMsg() {

    }

    //没活动的时候登录了加载活动
    public void reloadOreWarHp(){
        if(SystemOpenMgr.systemOpen(player, eSystemId.OreWar.getValue())){
            List<ActivityInfo> activityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
            for(ActivityInfo activityInfo : activityInfoList){
                initUserOreWarHp(activityInfo.getActivityId());
            }
        }
    }

    public void initOreWarSystem(){
        initUserOreData(player.getUserId());
        checkOreRefugeInit();
        List<ActivityInfo> activityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
        for(ActivityInfo activityInfo : activityInfoList){
            checkOreWarPatronsMap(activityInfo.getActivityId());
            syncUserOreWarPatronsData(activityInfo.getActivityId());
            initUserOreWarHp(activityInfo.getActivityId());
            syncUserOreWarHp(activityInfo.getActivityId());
        }
        syncData();
    }

    //自动增加hp和重置hp和重置活动
    private void autoChangeHp(int activityId){
        ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
        if(userOreWarHpMap.containsKey(activityId) && activityInfo != null){
            UserOreWarHp userOreWarHp = userOreWarHpMap.get(activityId);
            List<Integer> minuteList = StringUtils.stringToIntegerList(activityInfo.getParam5(),"\\|");//体力重置在当天的分钟|体力增加一点多少分钟
            long resetMinute = minuteList.get(0);
            long addHpMinute = minuteList.get(1);
            //每日重置在每天开启盐场的时候
            if(DateHelper.getTodayZeroTimeStamp() != DateHelper.getZeroTimeStamp(userOreWarHp.getTodayUpdate())){
                if(System.currentTimeMillis() >= DateHelper.getTodayZeroTimeStamp() + resetMinute*60*1000){
                    userOreWarHp.setHp(GameConfig.ACTIVITY_ORE_WAR_MAX_HP);
                    userOreWarHp.setTodayUpdate(System.currentTimeMillis());
                }
            }
            if(userOreWarHp.getLastUpdateTime() < DateHelper.getTodayZeroTimeStamp() + resetMinute*60*1000){
                userOreWarHp.setLastUpdateTime(DateHelper.getTodayZeroTimeStamp() + resetMinute*60*1000);
            }
            int addValue = (int)((System.currentTimeMillis() - userOreWarHp.getLastUpdateTime())/(addHpMinute*60*1000));
            if(addValue > 0){
                userOreWarHp.setHp(addValue + userOreWarHp.getHp());
                userOreWarHp.setLastUpdateTime(userOreWarHp.getLastUpdateTime() + addHpMinute*addValue*60*1000);
            }
        }
    }

    public void addHp(long value,int activityId){
        autoChangeHp(activityId);
        if(userOreWarHpMap.containsKey(activityId)){
            UserOreWarHp userOreWarHp = userOreWarHpMap.get(activityId);
            userOreWarHp.setHp(userOreWarHp.getHp() + (int)value);
            syncUserOreWarHp(activityId);
        }
    }

    public int useHp(int activityId){
        autoChangeHp(activityId);
        if(!userOreWarHpMap.containsKey(activityId)){
            return GameErrorCode.E_ORE_WAR_ACTIVITY_NO_FOUND;
        }
        UserOreWarHp userOreWarHp = userOreWarHpMap.get(activityId);
        if(userOreWarHp.getHp() <= 0){
            return GameErrorCode.E_ORE_WAR_NOT_ENOUGH_HP;
        }
        userOreWarHp.setHp(userOreWarHp.getHp() - 1);
        syncUserOreWarHp(activityId);
        return 0;
    }

    private void checkOreWarPatronsMap(int activityId){
        if(SystemOpenMgr.systemOpen(player, eSystemId.OreWar.getValue())){
            Map<Integer, UserPatrons> patronsMap = player.getModule(PatronsModule.class).getUserPatronsMap();
            for(UserPatrons userPatrons : patronsMap.values()){
                if(!oreWarPatronsDataMap.containsKey(activityId) || !oreWarPatronsDataMap.get(activityId).containsKey(userPatrons.getPatronsId())){
                    initUserOreWarPatronsData(userPatrons.getPatronsId(),activityId);
                }
            }
        }
    }


    //登录时候请求跨服同步下来后处理检验
    public void checkCrossPatronsDispatch(int activityId,List<CrossUserOrePit> crossUserOrePitList){
        //离线撤回修复数据后必须再次同步一次空坑位信息，和本服的不一样
        Map<Integer,CrossUserOrePit> crossUserOrePitMap = new ConcurrentHashMap<>();
        for(CrossUserOrePit crossUserOrePit : crossUserOrePitList){
            crossUserOrePitMap.put(crossUserOrePit.getPitId(),crossUserOrePit);
        }
        if(oreWarPatronsDataMap.containsKey(activityId)){
            for(UserOreWarPatronsData patronsData : oreWarPatronsDataMap.get(activityId).values()){
                if(patronsData.getDispatchOre() == OreWarMgr.DISPATCH_ORE_PUBLIC){
                    ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
                    if(activityInfo != null && activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
                        if(!crossUserOrePitMap.containsKey(patronsData.getPitId())){
                            patronsData.setPitId(0);
                            patronsData.setDispatchOre(0);
                        }
                    }
                }else if(patronsData.getDispatchOre() == OreWarMgr.DISPATCH_ORE_NONE){
                    if(patronsData.getPitId() != 0){
                        patronsData.setPitId(0);
                    }
                }else if(patronsData.getDispatchOre() == OreWarMgr.DISPATCH_ORE_SELF){
                    UserOreRefuge userOreRefuge = getUserOreRefuge(patronsData.getActivityId(), patronsData.getPitId());
                    if(null == userOreRefuge){
                        patronsData.setDispatchOre(0);
                    }
                }
            }
        }
    }

    //校验玩家占领
    private void checkPatronsDispatch(int activityId) {
        if(oreWarPatronsDataMap.containsKey(activityId)){
            for(UserOreWarPatronsData patronsData : oreWarPatronsDataMap.get(activityId).values()){
                if(patronsData.getDispatchOre() == OreWarMgr.DISPATCH_ORE_PUBLIC){
                    ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
                    if(activityInfo != null && activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
                        UserOrePit userOrePit = OreWarMgr.getUserOrePit(patronsData.getActivityId(), patronsData.getPitId());
                        if(userOrePit.getUserId() != player.getUserId()){
                            patronsData.setPitId(0);
                            patronsData.setDispatchOre(0);
                        }
                    }else if(activityInfo != null && activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
                        //改到登录请求自己坑位返回后去处理
                       /* CrossUserOrePit crossUserOrePit = OreWarMgr.getCrossUserOrePit(patronsData.getActivityId(), patronsData.getPitId());
                        if(OreWarMgr.isCrossUserOrePitSync(patronsData.getActivityId()) && crossUserOrePit.getUserId() != player.getUserId()){
                            patronsData.setPitId(0);
                            patronsData.setDispatchOre(0);
                        }*/
                    }
                }else if(patronsData.getDispatchOre() == OreWarMgr.DISPATCH_ORE_NONE){
                    if(patronsData.getPitId() != 0){
                        patronsData.setPitId(0);
                    }
                }else if(patronsData.getDispatchOre() == OreWarMgr.DISPATCH_ORE_SELF){
                    UserOreRefuge userOreRefuge = getUserOreRefuge(patronsData.getActivityId(), patronsData.getPitId());
                    if(null == userOreRefuge){
                        patronsData.setDispatchOre(0);
                    }
                }
            }
        }
    }

    public void syncOrePit(UserOrePit userOrePit, boolean isRemove){
        if(null != userOrePit){
            List<UserOrePit> syncList = new ArrayList<>();
            syncList.add(userOrePit);
            syncOrePit(userOrePit.getActivityId(),syncList, isRemove, false);
        }
    }

    public void syncCrossOrePit(CrossUserOrePit crossUserOrePit, boolean isRemove){
        if(null != crossUserOrePit){
            List<CrossUserOrePit> syncList = new ArrayList<>();
            syncList.add(crossUserOrePit);
            syncCrossOrePit(crossUserOrePit.getActivityId(),syncList, isRemove, false);
        }
    }

    public void syncCrossOrePit(int activityId,List<CrossUserOrePit> crossUserOrePitList, boolean isRemove){
        if(crossUserOrePitList.size() > 0){
            syncCrossOrePit(activityId, crossUserOrePitList, isRemove, false);
        }
    }

    private void syncOrePit(int activityId,List<UserOrePit> userOrePitList, boolean isRemove, boolean isLogin){
        if(isLogin) {
            player.sendPacket(Protocol.U_ORE_WAR_MY_PIT_LIST, OreWarPb.parseMyOrePitListMsg(activityId,userOrePitList, isRemove));
        }else {
            player.sendPacket(Protocol.U_ORE_WAR_MY_PIT_SYNC, OreWarPb.parseMyOrePitListMsg(activityId,userOrePitList, isRemove));
        }
    }

    public void syncCrossOrePit(int activityId,List<CrossUserOrePit> crossUserOrePitList, boolean isRemove, boolean isLogin){
        if(isLogin) {
            player.sendPacket(Protocol.U_ORE_WAR_MY_PIT_LIST, OreWarPb.parseMyCrossOrePitListMsg(activityId,crossUserOrePitList, isRemove));
        }else {
            player.sendPacket(Protocol.U_ORE_WAR_MY_PIT_SYNC, OreWarPb.parseMyCrossOrePitListMsg(activityId,crossUserOrePitList, isRemove));
        }
    }

    public void syncOreWarRecord(){
        List<ActivityInfo> activityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
        for(ActivityInfo activityInfo : activityInfoList){
            if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
                List<OreWarRecord> recordList = OreWarMgr.getUserOreWarRecord(activityInfo.getActivityId(),player.getUserId());
                OreWarProto.OreWarRecordListMsg.Builder respMsg = OreWarPb.parseOreWarRecordListMsg(recordList);
                player.sendPacket(Protocol.U_ORE_WAR_RECORD, respMsg);
            }else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
                List<CrossOreWarRecord> recordList = OreWarMgr.getCrossUserOreWarRecord(activityInfo.getActivityId(),player.getUserId());
                OreWarProto.OreWarRecordListMsg.Builder respMsg = OreWarPb.parseCrossOreWarRecordListMsg(recordList);
                player.sendPacket(Protocol.U_ORE_WAR_RECORD, respMsg);
            }

        }

    }

    public int getRefugeCount(int activityId){
        int count = 0;
        for(UserOreRefuge refugePit : userOreRefugeMap.get(activityId).values()){
            if(refugePit.getPatronsIdList().size() > 0){
                count += 1;
            }
        }
        return count;
    }

    public void syncUserOreWarHp(int activityId){
        autoChangeHp(activityId);
        if(userOreWarHpMap.containsKey(activityId)){
            UserOreWarHp userOreWarHp = userOreWarHpMap.get(activityId);
            player.sendPacket(Protocol.U_ORE_WAR_HP_SYNC, OreWarPb.parseOreWarHpMsg(userOreWarHp));
        }
    }

    //10点后撤回的時候同步一下
    public void syncOreRefuge(int activityId){
        syncOreRefuge(new ArrayList<>(userOreRefugeMap.get(activityId).values()));
    }

    private void syncOreRefuge(List<UserOreRefuge> userOreRefugeList){
        player.sendPacket(Protocol.U_ORE_WAR_REFUGE_DATA, OreWarPb.parseOreRefugeListMsg(userOreRefugeList));
    }

    //同步用户门客战斗信息,跨服下来会再同步一次
    public void syncUserOreWarPatronsData(int activityId){
        if(null != oreWarPatronsDataMap && oreWarPatronsDataMap.size() > 0){
            if(oreWarPatronsDataMap.containsKey(activityId)){
                syncUserOreWarPatronsData(new ArrayList<>(oreWarPatronsDataMap.get(activityId).values()));
            }
        }
    }

    private void syncUserOreWarPatronsData(){
        if(null != oreWarPatronsDataMap && oreWarPatronsDataMap.size() > 0){
            List<UserOreWarPatronsData> patronsList = new ArrayList<>();
            for (Map<Integer,UserOreWarPatronsData> patronsMap : oreWarPatronsDataMap.values()){
                patronsList.addAll(patronsMap.values());
            }
            syncUserOreWarPatronsData(patronsList);
        }
    }

    //获取占领门客的总坑位
    public int getOccupyCount(int activityId){
        List<String> pitIdList = new ArrayList<>();
        if(oreWarPatronsDataMap.containsKey(activityId)){
            for(UserOreWarPatronsData ud : new ArrayList<>(oreWarPatronsDataMap.get(activityId).values())){
                if(ud.getPitId() != 0 && !pitIdList.contains(ud.getDispatchOre() + "_" + ud.getPitId())){
                    pitIdList.add(ud.getDispatchOre() + "_" + ud.getPitId());
                }
            }
        }
        return pitIdList.size();
    }
    private void syncUserOreWarPatronsData(List<UserOreWarPatronsData> userOreWarPatronsDataList){
        player.sendPacket(Protocol.U_ORE_WAR_PATRONS_DATA_SYNC, OreWarPb.parseOreWarPatronsDataListMsg(userOreWarPatronsDataList));
    }

    private void syncUserOreWarPatronsData(UserOreWarPatronsData userOreWarPatronsData){
        if(null != userOreWarPatronsData){
            List<UserOreWarPatronsData> syncList = new ArrayList<>();
            syncList.add(userOreWarPatronsData);
            syncUserOreWarPatronsData(syncList);
        }
    }

    public void checkOreRefugeInit() {
        if(!SystemOpenMgr.systemOpen(player, eSystemId.OreWar.getValue())) {
            return;
        }
        List<ActivityInfo> activityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
        for(ActivityInfo activityInfo : activityInfoList){
            List<UserOreRefuge> syncList = new ArrayList<>();
            if(!userOreRefugeMap.containsKey(activityInfo.getActivityId())){
                userOreRefugeMap.put(activityInfo.getActivityId(),new ConcurrentHashMap<>());
            }
            Map<Integer, UserOreRefuge> userRefugeMap = userOreRefugeMap.get(activityInfo.getActivityId());
            for (int i = 0; i < GameConfig.MINE_REFUGE_PIT_COUNT; i++) {
                if (!userRefugeMap.containsKey(i + 1)) {
                    UserOreRefuge userOreRefuge = new UserOreRefuge();
                    userOreRefuge.setActivityId(activityInfo.getActivityId());
                    userOreRefuge.setPitId(i + 1);
                    userOreRefuge.setUserId(player.getUserId());
                    userOreRefuge.setJoinTime(0);
                    userOreRefuge.setLastGetRewardTime(0);
                    userOreRefuge.setPatronsIdList(new ArrayList<>());
                    userOreRefuge.setInsertOption();
                    userRefugeMap.put(userOreRefuge.getPitId(), userOreRefuge);
                    syncList.add(userOreRefuge);
                }
            }
            if(syncList.size() > 0){
                syncOreRefuge(syncList);
            }
        }
    }

    private void initUserOreWarPatronsData(int patronsId,int activityId) {
        if((!oreWarPatronsDataMap.containsKey(activityId) || !oreWarPatronsDataMap.get(activityId).containsKey(patronsId)) && player.getModule(PatronsModule.class).getUserPatronsMap().containsKey(patronsId)){
            UserOreWarPatronsData userOreWarPatronsData = new UserOreWarPatronsData();
            userOreWarPatronsData.setUserId(player.getUserId());
            userOreWarPatronsData.setPatronsId(patronsId);
            userOreWarPatronsData.setActivityId(activityId);
            userOreWarPatronsData.setBattleTimes(0);
            userOreWarPatronsData.setBuyBattleTimes(0);
            userOreWarPatronsData.setDispatchOre(0);
            userOreWarPatronsData.setModifyTime(System.currentTimeMillis() / 1000);
            userOreWarPatronsData.setInsertOption();
            if(!oreWarPatronsDataMap.containsKey(activityId)){
                oreWarPatronsDataMap.put(activityId,new ConcurrentHashMap<>());
            }
            oreWarPatronsDataMap.get(activityId).put(userOreWarPatronsData.getPatronsId(), userOreWarPatronsData);
        }
    }

    /**
     * 门客是否派驻中
     */
    private boolean isPatronsDispatchOre(int patronsId,int activityId){
        if(oreWarPatronsDataMap.containsKey(activityId)){
            UserOreWarPatronsData userOreWarPatronsData = oreWarPatronsDataMap.get(activityId).get(patronsId);
            if(null == userOreWarPatronsData){
                return false;
            }
            return userOreWarPatronsData.getDispatchOre() > 0;
        }
        return false;
    }

    //避难所占坑
    public OreWarProto.OreRefugeDispatchRespMsg.Builder refugeDispatch(int activityId, int pitId, List<Integer> patronsIdList) {
        OreWarProto.OreRefugeDispatchRespMsg.Builder msg = OreWarProto.OreRefugeDispatchRespMsg.newBuilder();
        ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo == null){
            msg.setRet(GameErrorCode.E_ORE_WAR_ACTIVITY_NO_FOUND);
            return msg;
        }
        if(patronsIdList.size() > GameConfig.MINE_WAR_BATTLE_MAX_PATRONS_COUNT){
            msg.setRet(GameErrorCode.E_ORE_WAR_PATRONS_COUNT_OVER);
            return msg;
        }
        Map<Integer, UserOreRefuge> refugeMap = userOreRefugeMap.get(activityId);
        if (null == refugeMap) {
            msg.setRet(GameErrorCode.E_ORE_WAR_ACTIVITY_NO_FOUND);
            return msg;
        }
        UserOreRefuge userOreRefuge = refugeMap.get(pitId);
        if(null == userOreRefuge){
            msg.setRet(GameErrorCode.E_ORE_WAR_ORE_REFUGE_NO_FOUND);
            return msg;
        }

        if(null != userOreRefuge.getPatronsIdList() && userOreRefuge.getPatronsIdList().size() > 0){
            msg.setRet(GameErrorCode.E_ORE_WAR_ORE_REFUGE_HAS_DISPATCH);
            return msg;
        }

        int ret = checkPatronsHasDispatch(patronsIdList,activityId);
        if(0 != ret){
            msg.setRet(ret);
            return msg;
        }
        int occupyCount = GameConfig.ACTIVITY_ORE_WAR_OCCUPY_LIMIT;
        if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
            occupyCount = player.getModule(OreWarModule.class).getOccupyCount(activityId);
        }else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
            //有bug，先这样
            occupyCount = player.getModule(OreWarModule.class).getOccupyCount(activityId);
        }
        if(occupyCount >= GameConfig.ACTIVITY_ORE_WAR_OCCUPY_LIMIT){
            msg.setRet(GameErrorCode.E_ORE_WAR_OCCUPY_LIMIT);
            return msg;
        }

        //消耗体力，不够报错
        int useHpRet = useHp(activityId);
        if(useHpRet != 0){
            msg.setRet(useHpRet);
            return msg;
        }
        SeasonTaskConditionData data = new SeasonTaskConditionData(BigInteger.valueOf((long) eActivityType.OreWarActivity.getValue()), BigInteger.ONE);
        player.notifyListener(eGamePlayerEventType.SaltCostStaminaTask.getValue(), data);

        long nowTime = System.currentTimeMillis() / 1000;
        userOreRefuge.setPatronsIdList(patronsIdList);
        userOreRefuge.setJoinTime(nowTime);
        userOreRefuge.setLastGetRewardTime(nowTime);
        updateOrePitPatronsState(patronsIdList, OreWarMgr.DISPATCH_ORE_SELF, activityId, pitId);
        OreWarProto.OreRefugeTempMsg.Builder refugeMsg = OreWarPb.parseOreRefugeTempMsg(userOreRefuge);
        if(null != refugeMsg){
            msg.setOreRefuge(refugeMsg);
        }
        //避难所也要五分钟的加成了
        long rewardTime = nowTime + GameConfig.MINE_WAR_VICTORY_REWARD;
        Property occupyReward = getPitOccupyReward(userOreRefuge, rewardTime, false);
        player.getModule(CurrencyModule.class).addCurrency(occupyReward, eLogMoneyType.OreWar, eLogMoneyType.OreWarRefugeReward);
        int notifyType = eGamePlayerEventType.OreWarAcCoinOutPut.getValue();
        int eLogType = eLogMoneyType.OreWarRefugeReward.getValue();
        if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
            notifyType = eGamePlayerEventType.CrossOreWarAcCoinOutPut.getValue();
            eLogType = eLogMoneyType.CrossOreWarRefugeReward.getValue();
        }
        OreWarActivityMgr.conditionNotify(player.getUserId(),notifyType,
                new OreWarActivityUnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, occupyReward.getCountByGoodsId(GameConfig.ACTIVITY_ORE_WAR_OUTPUT_GOODSID),
                        player.getUserInfo().getUnionUid(),activityId));
        LogMgr.addLogOreWarReward(player.getUserId(), activityInfo.getActivityId(), pitId, PropertyHelper.parsePropertyToString(occupyReward), GameConfig.MINE_WAR_VICTORY_REWARD,
                true, eLogType);
        msg.setWinReward(PropertyHelper.parsePropertyToString(occupyReward));
        msg.setRet(0);
        return msg;
    }

    //避难所撤回
    public OreWarProto.OreRefugeRecallRespMsg.Builder refugeRecall(int activityId, int pitId) {
        OreWarProto.OreRefugeRecallRespMsg.Builder msg = OreWarProto.OreRefugeRecallRespMsg.newBuilder();
        Map<Integer, UserOreRefuge> refugeMap = userOreRefugeMap.get(activityId);
        if (null == refugeMap) {
            msg.setRet(GameErrorCode.E_ORE_WAR_ACTIVITY_NO_FOUND);
            return msg;
        }
        UserOreRefuge userOreRefuge = refugeMap.get(pitId);
        if(null == userOreRefuge){
            msg.setRet(GameErrorCode.E_ORE_WAR_ORE_REFUGE_NO_FOUND);
            return msg;
        }
        if(null == userOreRefuge.getPatronsIdList()){
            msg.setRet(GameErrorCode.E_ORE_WAR_ORE_REFUGE_NO_DISPATCH);
            return msg;
        }
        if(userOreRefuge.getPatronsIdList().size() == 0){
            msg.setRet(GameErrorCode.E_ORE_WAR_ORE_REFUGE_NO_DISPATCH);
            return msg;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        long lastGetRewardTime = userOreRefuge.getLastGetRewardTime();
        Property reward = getPitOccupyReward(userOreRefuge, nowTime, true);
        //资源入袋
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.OreWar, eLogMoneyType.OreWarRefugeRecall);
        ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
        int eventType = 0;
        if(activityInfo != null && activityInfo.getChildType() == 1){
            eventType = eGamePlayerEventType.OreWarAcCoinOutPut.getValue();
        }else if(activityInfo != null && activityInfo.getChildType() == 2){
            eventType = eGamePlayerEventType.CrossOreWarAcCoinOutPut.getValue();
        }
        OreWarActivityMgr.conditionNotify(player.getUserId(),eventType,
                new OreWarActivityUnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, reward.getCountByGoodsId(GameConfig.ACTIVITY_ORE_WAR_OUTPUT_GOODSID),
                        player.getUserInfo().getUnionUid(), activityId));
        LogMgr.addLogOreWarReward(player.getUserId(), userOreRefuge.getActivityId(), userOreRefuge.getPitId(), PropertyHelper.parsePropertyToString(reward),
                nowTime - lastGetRewardTime, true, eLogMoneyType.OreWarRefugeRecall.getValue());

        //清除状态
        updateOrePitPatronsState(userOreRefuge.getPatronsIdList(), OreWarMgr.DISPATCH_ORE_NONE, activityId, 0);
        userOreRefuge.setPatronsIdList(new ArrayList<>());
        msg.setReward(reward.getCountByGoodsId(GameConfig.ACTIVITY_ORE_WAR_OUTPUT_GOODSID).longValue());
        OreWarProto.OreRefugeTempMsg.Builder refugeMsg = OreWarPb.parseOreRefugeTempMsg(userOreRefuge);
        if(null != refugeMsg){
            msg.setOreRefuge(refugeMsg);
        }
        msg.setRet(0);
        return msg;
    }

    //每日时间到点了清理掉玩家的所有避难所数据
    public Property refugeRecallAll(int activityId) {
        Map<Integer, UserOreRefuge> refugeMap = userOreRefugeMap.get(activityId);
        Property allProperty = new Property();
        for(UserOreRefuge userOreRefuge : refugeMap.values()){
            if(userOreRefuge.getPatronsIdList() != null && userOreRefuge.getPatronsIdList().size() > 0){
                long nowTime = System.currentTimeMillis() / 1000;
                long lastGetRewardTime = userOreRefuge.getLastGetRewardTime();
                Property reward = getPitOccupyReward(userOreRefuge, nowTime, true);
                allProperty.addProperty(reward);
                updateOrePitPatronsState(userOreRefuge.getPatronsIdList(), OreWarMgr.DISPATCH_ORE_NONE, activityId, 0);
                userOreRefuge.setPatronsIdList(new ArrayList<>());
                LogMgr.addLogOreWarReward(player.getUserId(), userOreRefuge.getActivityId(), userOreRefuge.getPitId(), PropertyHelper.parsePropertyToString(reward),
                        nowTime - lastGetRewardTime, true, eLogMoneyType.OreWarRefugeRecall.getValue());
            }
        }

        return allProperty;
    }


    public Property getOccupyReward(int activityId,long rewardTime, int patronsId) {
        Property occupyReward = new Property();
        for (UserOreRefuge userOreRefuge : userOreRefugeMap.get(activityId).values()) {
            if(userOreRefuge.getPatronsIdList().size() > 0) {
                boolean isReward = false;
                if(0 == patronsId || (patronsId > 0 && userOreRefuge.getPatronsIdList().contains(patronsId))){
                    isReward = true;
                }
                if(isReward) {
                    long lastGetRewardTime = userOreRefuge.getLastGetRewardTime();
                    Property reward = getPitOccupyReward(userOreRefuge, rewardTime, true);
                    occupyReward.addProperty(reward);
                    LogMgr.addLogOreWarReward(player.getUserId(), userOreRefuge.getActivityId(), userOreRefuge.getPitId(), PropertyHelper.parsePropertyToString(occupyReward),
                            rewardTime - lastGetRewardTime, true, eLogMoneyType.OreWarGetOccupyReward.getValue());
                }
            }
        }
        return occupyReward;
    }

    //获取广场的奖励
    private Property getPitOccupyReward(UserOreRefuge userOreRefuge, long rewardTime, boolean isUpdateRewardTime) {
        Property occupyReward = new Property();
        if (null == userOreRefuge) {
            return occupyReward;
        }
        if(userOreRefuge.getPatronsIdList().size() <= 0){
            return occupyReward;
        }
        long occupyTime = (rewardTime - userOreRefuge.getLastGetRewardTime()) / 60;
        if(occupyTime > 0) {//算钱咯
            List<UserPatrons> patronsList = player.getModule(PatronsModule.class).getUserPatronsList(userOreRefuge.getPatronsIdList());
            BigInteger oreOutput = OreWarMgr.getOreOutput(player.getUserId(), userOreRefuge.getActivityId(), patronsList, GameConfig.ACTIVITY_ORE_WAR_SQUARE_PROFIT,0).multiply(BigDecimal.valueOf(occupyTime)).toBigInteger();
            int goodsId = GameConfig.ACTIVITY_ORE_WAR_OUTPUT_GOODSID;
            if (goodsId > 0) {
                occupyReward.addProperty(goodsId, oreOutput);
            }
            if(isUpdateRewardTime){
                userOreRefuge.setLastGetRewardTime(rewardTime);
            }
        }
        return occupyReward;
    }

    private UserOreRefuge getUserOreRefuge(int activityId,int pitId){
        if(userOreRefugeMap.containsKey(activityId)){
            return userOreRefugeMap.get(activityId).get(pitId);
        }
        return null;
    }

    public int getDispatchOrePitCount(int activityId){
        int count = 0;
        for(Map.Entry<Integer, Map<Integer, UserOreRefuge>> dateEntry : userOreRefugeMap.entrySet()){
            for(UserOreRefuge userOreRefuge : dateEntry.getValue().values()){
                if(userOreRefuge.getPatronsIdList().size() > 0){
                    count++;
                }
            }
        }
        List<UserOrePit> userOrePitList = OreWarMgr.getUserOrePitList(activityId,player.getUserId());
        count = count + userOrePitList.size();
        return count;
    }

    private int checkPatronsHasDispatch(List<Integer> patronsIdList,int activityId) {
        for (int patronsId : patronsIdList) {
            UserOreWarPatronsData patronsData = getUserOreWarPatronsData(patronsId,activityId);
            if(null == patronsData){
                return GameErrorCode.E_PATRONS_NO_FOUND;
            }
            if(patronsData.getDispatchOre() > 0){
                return GameErrorCode.E_ORE_WAR_PATRONS_HAS_DISPATCH;
            }
        }
        return 0;
    }

    public void resetOneDay(){
        List<ActivityInfo> activityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
        for(ActivityInfo activityInfo : activityInfoList){
            long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000;
            List<UserOreWarPatronsData> syncList = new ArrayList<>();
            if(oreWarPatronsDataMap.containsKey(activityInfo.getActivityId())){
                for(UserOreWarPatronsData userOreWarPatronsData : oreWarPatronsDataMap.get(activityInfo.getActivityId()).values()){
                    if(userOreWarPatronsData.getModifyTime() < zeroTime){
                        userOreWarPatronsData.setBattleTimes(0);
                        userOreWarPatronsData.setBuyBattleTimes(0);
                        userOreWarPatronsData.setModifyTime(zeroTime);
                        syncList.add(userOreWarPatronsData);
                    }
                }
            }
            if(syncList.size() > 0){
                syncUserOreWarPatronsData(syncList);
            }
        }
    }

    public int checkAndCostBattleTimes(List<Integer> patronsIdList,int activityId) {
        int ingots = 0;         //消耗元宝数
        List<Integer> needBuyTimesPatronsId = new ArrayList<>();
        for (int patronsId : patronsIdList) {
            UserOreWarPatronsData patronsData = getUserOreWarPatronsData(patronsId,activityId);
            if(null == patronsData){
                return GameErrorCode.E_PATRONS_NO_FOUND;
            }
            if(patronsData.getDispatchOre() > 0){
                return GameErrorCode.E_ORE_WAR_PATRONS_HAS_DISPATCH;
            }
            int maxBattleTimes = getMaxOreWarBattleTimes();
            //挑战次数不限制了
            if (patronsData.getBattleTimes() >= maxBattleTimes) {
                if(patronsData.getBattleTimes() >= maxBattleTimes + GameConfig.MINE_WAR_PATRONS_MAX_BUY_BATTLE_TIMES){
                    //超过挑战次数了
//                    return GameErrorCode.E_ORE_WAR_BATTLE_TIMES_OVER;
                }else {
//                    needBuyTimesPatronsId.add(patronsId);
//                    ingots += GameConfig.MINE_WAR_PATRONS_BUY_BATTLE_TIMES_BASE_COST + GameConfig.MINE_WAR_PATRONS_BUY_BATTLE_TIMES_ADD_COST * patronsData.getBuyBattleTimes();
                }
            }
        }
        long nowTime = System.currentTimeMillis() / 1000;
        List<UserOreWarPatronsData> syncList = new ArrayList<>();
        if (0 == ingots) {
            for(int patronsId : patronsIdList){
                UserOreWarPatronsData patronsData = getUserOreWarPatronsData(patronsId,activityId);
                patronsData.setBattleTimes(patronsData.getBattleTimes() + 1);
                patronsData.setModifyTime(nowTime);
                syncList.add(patronsData);
            }
            for(int patronsId : needBuyTimesPatronsId){
                UserOreWarPatronsData patronsData = getUserOreWarPatronsData(patronsId,activityId);
                patronsData.setBuyBattleTimes(patronsData.getBuyBattleTimes() + 1);
                patronsData.setModifyTime(nowTime);
            }
            syncUserOreWarPatronsData(syncList);
            return 0;
        }else {
            if (!player.getModule(CurrencyModule.class).removeCurrency(new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(ingots)),
                    eLogMoneyType.OreWar, eLogMoneyType.OreWarBuyBattleTimes)) {//元宝不足
                return GameErrorCode.E_BAG_INGOTS_NO_ENOUGH;
            }
            for(int patronsId : patronsIdList){
                UserOreWarPatronsData patronsData = getUserOreWarPatronsData(patronsId,activityId);
                patronsData.setBattleTimes(patronsData.getBattleTimes() + 1);
                patronsData.setModifyTime(nowTime);
                syncList.add(patronsData);
            }
            for(int patronsId : needBuyTimesPatronsId){
                UserOreWarPatronsData patronsData = getUserOreWarPatronsData(patronsId,activityId);
                patronsData.setBuyBattleTimes(patronsData.getBuyBattleTimes() + 1);
                patronsData.setModifyTime(nowTime);
            }
            syncUserOreWarPatronsData(syncList);
            return 0;
        }
    }

    //把所有的坑位上面的占领门客信息清掉t_u_activity_orewar_patrons上dispatchOre=1的清掉，就不跨服上面同步下来了
    public void clearOrePitPatronsState(int activityId){
        List<UserOreWarPatronsData> syncList = new ArrayList<>();
        for(UserOreWarPatronsData patronsData : oreWarPatronsDataMap.get(activityId).values()){
            if(patronsData.getDispatchOre() == 1){
                patronsData.setDispatchOre(OreWarMgr.DISPATCH_ORE_NONE);
                patronsData.setActivityId(activityId);
                patronsData.setPitId(0);
                syncList.add(patronsData);
            }
        }
        syncUserOreWarPatronsData(syncList);
    }

    public void updateOrePitPatronsState(List<Integer> patronsIdList, int dispatchOre, int activityId, int pitId) {
        List<UserOreWarPatronsData> syncList = new ArrayList<>();
        for(int patronsId : patronsIdList){
            UserOreWarPatronsData patronsData = getUserOreWarPatronsData(patronsId,activityId);
            patronsData.setDispatchOre(dispatchOre);
            patronsData.setActivityId(activityId);
            patronsData.setPitId(pitId);
            syncList.add(patronsData);
        }
        syncUserOreWarPatronsData(syncList);
    }

    public void notifyAddPatrons(UserPatrons userPatrons){
        if(SystemOpenMgr.systemOpen(player, eSystemId.OreWar.getValue())) {
            List<ActivityInfo> activityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
            for(ActivityInfo activityInfo : activityInfoList){
                UserOreWarPatronsData patronsData = getUserOreWarPatronsData(userPatrons.getPatronsId(),activityInfo.getActivityId());
                if (null != patronsData) {
                    syncUserOreWarPatronsData(patronsData);
                }
            }
        }
    }

    public void patronsAbilityChangeAdd(List<UserPatrons> patronsList) {
        synchronized (needUpdateMap) {
            for (UserPatrons userPatrons : patronsList) {
                needUpdateMap.put(userPatrons.getPatronsId(), userPatrons);
            }
        }
    }

    public void patronsAbilityChangeTimeSync() {
        List<UserPatrons> patronsList;
        synchronized (needUpdateMap) {
            patronsList = new ArrayList<>(needUpdateMap.values());
            needUpdateMap.clear();
        }
        if (patronsList.size() > 0) {
            patronsAbilityChange(patronsList);
        }
    }

    public void patronsAbilityChange(List<UserPatrons> patronsList){
        List<ActivityInfo> activityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
        for(ActivityInfo activityInfo : activityInfoList){
            patronsAbilityChange(patronsList,activityInfo.getActivityId());
        }
    }

    public void patronsAbilityChange(List<UserPatrons> patronsList,int activityId){
        for(UserPatrons userPatrons : patronsList) {
            UserOreWarPatronsData patronsData = getUserOreWarPatronsData(userPatrons.getPatronsId(),activityId);
            if(patronsData.getDispatchOre() == OreWarMgr.DISPATCH_ORE_PUBLIC) {
                ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
                if(activityInfo != null){
                    if(activityInfo.getChildType() == eActivityOreWarType.OreWar.getChildType()){
                        OreWarMgr.addTask(patronsData.getActivityId(),new PatronsAbilityChangeTask(player, patronsData.getActivityId(), patronsData.getPitId(), userPatrons));
                    }/*else if(activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()){
                        OreWarMgr.addCrossTask(patronsData.getActivityId(),new PatronsAbilityChangeTask(player, patronsData.getActivityId(), patronsData.getPitId(), userPatrons));
                    }*/
                }
            }
        }
        ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
        if (activityInfo != null && activityInfo.getChildType() == eActivityOreWarType.CrossOreWar.getChildType()) {
            Map<Integer, List<UserPatrons>> pitPatronsMap = new HashMap<>();
            for (UserPatrons userPatrons : patronsList) {
                UserOreWarPatronsData patronsData = getUserOreWarPatronsData(userPatrons.getPatronsId(),activityId);
                if(patronsData.getDispatchOre() == OreWarMgr.DISPATCH_ORE_PUBLIC && patronsData.getActivityId() == activityId) {
                    int pitId = patronsData.getPitId();
                    if (!pitPatronsMap.containsKey(pitId)) {
                        pitPatronsMap.put(pitId, new ArrayList<>());
                    }
                    List<UserPatrons> list = pitPatronsMap.get(pitId);
                    list.add(userPatrons);
                }
            }
            int groupId = GameUnionActivityGroupMgr.getGroupId(activityId, getUserId());
            OreWarMgr.addCrossTask(activityId, new CrossOrePatronsAbilityChangeTask(player, activityId, pitPatronsMap),groupId);
        }
    }

    private UserOreWarPatronsData getUserOreWarPatronsData(int patronsId,int activityId){
        initUserOreWarPatronsData(patronsId,activityId);
        return oreWarPatronsDataMap.get(activityId).get(patronsId);
    }

    //获取对应坑位门客的占领情况
    public List<UserPatrons> getPitUserPatronsList(int activityId,int pitId){
        List<UserPatrons> userPatronsList = new ArrayList<>();
        for(UserOreWarPatronsData up : oreWarPatronsDataMap.get(activityId).values()){
            if(up.getPitId() == pitId && up.getDispatchOre() == OreWarMgr.DISPATCH_ORE_PUBLIC){
                UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(up.getPatronsId());
                userPatronsList.add(userPatrons);
            }
        }
        return userPatronsList;
    }

    public UserOreData getUserOreData() {
        return userOreData;
    }

    private int getMaxOreWarBattleTimes(){
        return GameConfig.MINE_WAR_PATRONS_DEFAULT_BATTLE_TIMES + player.getModule(CardModule.class).oreWarAddBattleTimes();
    }

    private void initUserOreData(long userId) {
        if(null == userOreData) {
            UserOreData oreData = new UserOreData();
            oreData.setUserId(userId);
            oreData.setTotalRobTimes(0);
            oreData.setTotalBeRobTimes(0);
            oreData.setMaxTeamReward1(BigInteger.ZERO);
            oreData.setMaxTeamReward2(BigInteger.ZERO);
            oreData.setMaxTeamReward3(BigInteger.ZERO);
            oreData.setTotalReward1(BigInteger.ZERO);
            oreData.setTotalReward2(BigInteger.ZERO);
            oreData.setTotalReward3(BigInteger.ZERO);
            oreData.setInsertOption();
            userOreData = oreData;
            OreWarMgr.setUserOreData(userOreData);
        }
    }

    private void initUserOreWarHp(int activityId) {
        ActivityInfo activityInfo = OreWarActivityMgr.getOpenActivityInfo(activityId);
        if(activityInfo != null && !userOreWarHpMap.containsKey(activityId)) {
            List<Integer> minuteList = StringUtils.stringToIntegerList(activityInfo.getParam5(),"\\|");//体力重置在当天的分钟|体力增加一点多少分钟
            long resetMinute = minuteList.get(0);
            UserOreWarHp temp = new UserOreWarHp();
            temp.setUserId(player.getUserId());
            temp.setActivityId(activityId);
            temp.setHp(GameConfig.ACTIVITY_ORE_WAR_MAX_HP);
            temp.setLastUpdateTime(DateHelper.getTodayZeroTimeStamp() + resetMinute*60*1000);
            temp.setTodayUpdate(System.currentTimeMillis());
            temp.setInsertOption();
            userOreWarHpMap.put(activityId,temp);
        }
    }
}
